package com.hspedu.web.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class MyIntercepter01 implements HandlerInterceptor {
    /**
     * 目标法执行前执行，返回false，目标方法不再执行
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("第一个拦截器MyIntercepter01111");
        String keyword = request.getParameter("keyword");
        if ("病毒".equals(keyword)){
            request.getRequestDispatcher("/WEB-INF/pages/warining.jsp")
                    .forward(request,response);
            return false;
        }
        System.out.println("得到的Keyword="+keyword);
        return true;
    }

    /**
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("---MyIntercepter01(postHandle)---");
    }

    /**
     * 视图渲染后被执行
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("------MyIntercepter01(afterCompletion)-------");
    }
}
